package social;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import QuizPackage.QuizDBC;

/**
 * Servlet implementation class AdminServlet
 */
@WebServlet("/AdminServlet")
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		SocialDBC db = (SocialDBC) getServletContext().getAttribute("database");
		QuizDBC qdb = (QuizDBC) getServletContext().getAttribute("quizDatabase");
		HttpSession session = request.getSession();
		String action = request.getParameter("action");
		String userAction = request.getParameter("userAction");
		
		if (action.equals("removeUser")) {
			db.removeUser(userAction);
			//request.setAttribute("profileName", session.getAttribute("username"));
			ArrayList<Integer> remove = qdb.getQuizListByCreator(userAction);
			qdb.removeResults(userAction);
			for(int i=0 ; i < remove.size() ; i++)
			{
				qdb.deleteQuiz(remove.get(i));
			}
			db.removeAcheivements(userAction);
			session.setAttribute("profileName", session.getAttribute("username"));
		} else if (action.equals("promoteAdmin")) {
			db.promoteAdmin(userAction);
			//request.setAttribute("profileName", userAction);
			session.setAttribute("profileName", userAction);
		} else if (action.equals("removeAdmin")) {
			db.removeAdmin(userAction);
			//request.setAttribute("profileName", userAction);
			session.setAttribute("profileName", userAction);
		} else if (action.equals("madeAnnouncement")) {
			//System.out.println("here");
			db.addAnnouncement((String)session.getAttribute("username"), 
					request.getParameter("announcement"));
			//request.setAttribute("profileName", session.getAttribute("username"));
			session.setAttribute("profileName", session.getAttribute("username"));
		} else if (action.equals("removeQuiz")) {
			int quizid = Integer.parseInt(request.getParameter("quizid"));
			qdb.deleteQuiz(quizid);
			session.setAttribute("profileName", session.getAttribute("username"));
		} else if (action.equals("removeResults")){
			int quizid = Integer.parseInt(request.getParameter("quizid"));
			qdb.removeQuizResults(quizid);
			session.setAttribute("profileName", session.getAttribute("username"));
		}
		
		//request.setAttribute("profileName", session.getAttribute("username"));
		//request.getRequestDispatcher("user-profile.jsp").forward(request, response);
		response.sendRedirect("user-profile.jsp");
	}
}
